package com.xyb.service;

import com.xyb.config.exception.MyException;
import com.xyb.dao.SysMenuDao;
import com.xyb.dao.SysMenuRoleDao;
import com.xyb.entity.StatusCode;
import com.xyb.entity.SysMenu;
import com.xyb.entity.SysMenuRole;
import com.xyb.utils.IdUtils;
import com.xyb.vo.SysMenuVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenuService {
    @Resource
    private SysMenuDao sysMenuDao;
    @Resource
    private IdUtils idUtils;
    @Resource
    private SysMenuRoleDao sysMenuRoleDao;

    String[] menus = new String[]{"sys", "user", "role", "menu"};

    public List<SysMenu> selectSysMenuByUserId(Long userId) {
        List<SysMenu> menuList = sysMenuDao.selectSysMenuByUserId(userId);
        List<SysMenu> list = new ArrayList<>();

        for (int i = 0; i < menuList.size(); i++) {
            SysMenu item = menuList.get(i);
            if (item.getMenuPid().intValue() == -1) {
                list.add(item);
            }
        }

        for (int i = 0; i < list.size(); i++) {
            SysMenu item = list.get(i);
            Long pId = item.getMenuId();
            List<SysMenu> tmpList = new ArrayList<>();

            for (int j = 0; j < menuList.size(); j++) {
                SysMenu em = menuList.get(j);
                if (em.getMenuPid().intValue() == pId.intValue()) {
                    tmpList.add(em);
                }
            }
            item.setMenuList(tmpList);
        }
        return list;
    }

    public List<SysMenu> selectSysMenu() {
        List<SysMenu> list = sysMenuDao.selectSysMenuListByPid(-1L);
        list.forEach(i -> {
            List<SysMenu> sysMenusList = sysMenuDao.selectSysMenuListByPid(i.getMenuId());
            i.setMenuList(sysMenusList);
        });
        return list;
    }

    public String insertSysMenuByMenuPid(SysMenuVo sysMenuVo) {
        Long menuPid = sysMenuVo.getMenuPid();
        Long max = sysMenuDao.selectMaxShowOrderByPid(menuPid);
        long nextId = idUtils.nextId();
        SysMenu sysMenu = SysMenu.builder()
                .menuId(nextId)
                .menuName("临时节点")
                .menuCode("menuCode")
                .menuIcon("")
                .menuPid(menuPid)
                .showOrder(max == null ? 0 : max + 1)
                .build();
        sysMenuDao.insertSysMenu(sysMenu);
        //添加到admin
        SysMenuRole sysMenuRole = SysMenuRole.builder()
                .menuRoleId(idUtils.nextId())
                .menuId(nextId)
                .roleId(0L)
                .build();
        sysMenuRoleDao.insertSysMenuRole(sysMenuRole);
        return "success";
    }

    public String updateSysMenuByMenuId(SysMenuVo vo) {
        //如果更新的是 sys，user，role，menu 不可更新
        SysMenu menu = sysMenuDao.selectSysMenuByMenuId(vo.getMenuId());
        String menuCode = vo.getMenuCode();
        for (int i = 0; i < menus.length; i++) {
            if (menus[i].equals(menu.getMenuCode())) {
                menuCode = menu.getMenuCode();
            }
        }
        //更新信息
        SysMenu sysMenu = SysMenu.builder()
                .menuId(vo.getMenuId())
                .menuName(vo.getMenuName())
                .menuCode(menuCode)
                .menuIcon(vo.getMenuIcon())
                .build();
        sysMenuDao.updateSysMenuByMenuId(sysMenu);
        return "success";
    }

    public String deleteSysMenuByMenuId(Long menuId) {
        //查询节点信息
        SysMenu sysMenu = sysMenuDao.selectSysMenuByMenuId(menuId);
        //管理员角色不可以删除
        boolean flag = false;
        for (int i = 0; i < menus.length; i++) {
            if (menus[i].equals(sysMenu.getMenuCode())) {
                flag = true;
            }
        }
        if (menuId.intValue() == 0 || flag) {
            throw new MyException(StatusCode.ADMIN_REFUSED_EXCEPTION);
        }
        if (sysMenu.getMenuPid().intValue() == -1) {     //如果是-1说明是第一层子菜单,删除子菜单
            List<SysMenu> sysMenuList = sysMenuDao.selectSysMenuListByPid(menuId);
            sysMenuList.forEach(i -> {
                sysMenuRoleDao.deleteSysMenuByMenuId(i.getMenuId());
            });
            sysMenuDao.deleteSysMenuByMenuPid(menuId);
        }
        sysMenuDao.deleteSysMenuByMenuId(menuId);
        sysMenuRoleDao.deleteSysMenuByMenuId(menuId);
        return "success";
    }

    public String updateSysMenuShowOrder(Long[] menuIds) {
        List<SysMenu> list = new ArrayList<>();
        for (int i = 0; i < menuIds.length; i++) {
            SysMenu sysMenu = new SysMenu();
            sysMenu.setMenuId(menuIds[i]);
            sysMenu.setShowOrder((long)i);
            list.add(sysMenu);
        }
        sysMenuDao.updateSysMenuBySysMenuList(list);
        return "success";
    }
}















































